package com.wanbangee.marksystem.service.imp;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wanbangee.marksystem.dao.SScoreDao;
import com.wanbangee.marksystem.dao.SScoreDetailDao;
import com.wanbangee.marksystem.entities.MExamMarker;
import com.wanbangee.marksystem.entities.SScoreDetail;
import com.wanbangee.marksystem.service.QualityCheckService;

@Service
public class QualityCheckServiceImp implements QualityCheckService{

	@Autowired
	private SScoreDetailDao sScoreDetailDao;
	
	@Autowired
	private SScoreDao sScoreDao;
	
	@Override
	public MExamMarker selectInfoByExamIdSubIdTopicNo(Integer examId, Integer examSubId,
			Integer topicNo, MExamMarker mExamMarker) {
		List<SScoreDetail> sscoreDetails = sScoreDetailDao.selectScoreByMarkerId(mExamMarker.getMarkerId(), examId, examSubId, mExamMarker.getScoreType());
		mExamMarker.setScoreDetails(sscoreDetails);
		System.out.println(sscoreDetails.size());
		return mExamMarker;
	}

	/**
	 * 查询总体的平均分、最高分、最小分
	 */
	@Override
	public Map selectAllAvgMaxMin(Integer examId, Integer examSubId, Integer topicNo, Integer scoreType) {
		return sScoreDetailDao.selectAllAvgMaxMin(examId, examSubId, topicNo, scoreType);
	}

	/**
	 * 查询质量监控老师阅卷的成绩的详细信息
	 */
	@Override
	public List<SScoreDetail> selectSScoreDetails(Integer examId, Integer examSubId, Integer topicNo, Integer markerId,
			Integer order, Integer scoreType) {
		
		List<SScoreDetail> sScoreDetails = sScoreDetailDao.selectScoreByOrderAndMarkerId(examId, examSubId, topicNo, markerId, order, scoreType);
		for (int i = 0; i < sScoreDetails.size(); i++) {
			sScoreDetails.get(i).setsScore(sScoreDao.selectSScoreTopicUrlByScoreId(sScoreDetails.get(i).getScoreId(), examId, examSubId));
		}
		return sScoreDetails;
	}

	/**
	 * 打回重评
	 */
	@Override
	public Integer updateCheckScore(Integer examId, Integer examSubId, Integer scoreId, String repeatCause) {
		// TODO Auto-generated method stub
		//1. 更新阅卷表（更新评阅状态）
		Integer result1 = sScoreDao.updateSScoreBySScoreId(examId, examSubId, scoreId, repeatCause);
		//2. 更新阅卷详细表
		//	将分数都恢复到默认值
		Integer result2 = sScoreDetailDao.updateSScoreDetailByScoreId(examId, examSubId, scoreId);
		return result1==result2?1:0;
	}

}
